//
//  MGECCV.m
//
//  Created by HeLei on 2018/11/22.
//  Copyright © 2018年 电力设备. All rights reserved.
//
#define MGECColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#import "MGPickerView.h"

@interface MGPickerView()<UIPickerViewDataSource,UIPickerViewDelegate>{
    UIView *bgView;
    NSInteger  rowNumber;
}

@property (nonatomic,strong)NSArray *contentArr;
@end



@implementation MGPickerView
- (id)initWithFrame:(CGRect)frame withDataArr:(NSArray *)arr{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        _contentArr = arr;
        
         
        [self initPickerView];
    }
    return  self;
}

- (void)initPickerView {
    
    //bg
    
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0,self.frame.size.height*2/3 ,[UIScreen mainScreen].bounds.size.width ,self.frame.size.height/3 )];
    bgView.backgroundColor = MGECColorFromRGB(0xfefefe);
    
    
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(event:)];
    [self addGestureRecognizer:tapGesture];
    [self addSubview:bgView];
    
    
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,30 , [UIScreen mainScreen].bounds.size.width, bgView.frame.size.height - 30)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    
    [bgView addSubview:_pickerView];
    
    
    
    for (NSInteger i = 0; i < 2; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        CGRect frame = CGRectMake(i?bgView.frame.size.width-75:15, 15, 60,30);
        btn.frame = frame;
        btn.tag = i;
        [btn addTarget:self action:@selector(chooseCarTypeSureAndUndoBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [btn setTitle:i?@"确定":@"取消" forState:UIControlStateNormal];
        [btn.layer setCornerRadius:5];
        [btn setTitleColor:MGECColorFromRGB(0xffffff) forState:UIControlStateNormal];
        btn.backgroundColor = MGECColorFromRGB(0x3CACEC);
        
        [bgView addSubview:btn];
    }
}

#pragma mark --pickerDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.contentArr.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    _carTypeStr = self.contentArr[0];//不滑动时也能选择
    rowNumber = row;
    return self.contentArr[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _carTypeStr = self.contentArr[row];
    rowNumber = row;
}

- (void)chooseCarTypeSureAndUndoBtnAction:(UIButton *)sender {
    
    if (sender.tag == 1) {
        self.carTypeStrIndex = rowNumber;
    }
    
    if ([self.delegate respondsToSelector:@selector(chooseCarTypeSureAndUndoBtnAction:)]) {
        
        [self.delegate chooseCarTypeSureAndUndoBtnAction:sender];
        
    }
    
}
- (void)event:(UITapGestureRecognizer *)gesture

{
    if ([self.delegate respondsToSelector:@selector(changePickerFrame)]) {
        [self.delegate changePickerFrame];
    }
}

@end
